﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oplaco.Web.DTO;
using Oplaco.Core.BusinessLogicLayer;
using System.Configuration;

public partial class Controls_Admin_ManagerAdversitmentsControl : System.Web.UI.UserControl
{
    private Oplaco.Core.Configuration.SiteSettings settings = Oplaco.Core.Configuration.SiteSettings.GetSharedSettings();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            lstCategory.InitialListItem = "---Select---";
            this.loadAdTypeListBox();
            this.loadAdLevelListBox();
            AdvertismentManager.ClearCache(HttpContext.Current);
            CachedAdvertismentCollection alist = AdvertismentManager.Fetch(100);
            if (alist != null && alist.Count > 0)
            {
                AdsList.DataSource = alist;
                AdsList.DataBind();                
            }

        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        CheckBox cb = (CheckBox)sender;
        AdLevel level = AdLevel.Normal;
        if (cb.Checked) level = AdLevel.Featured; 

        ListViewItem item = (ListViewItem)cb.NamingContainer;
        ListViewDataItem dataItem = (ListViewDataItem)item;
        Int32 id = Int32.Parse(AdsList.DataKeys[dataItem.DisplayIndex].Value.ToString());
                
        AdsDB.UpdateAdLevel(id, level);
    }


    protected void btnSearch_Click(object sender, EventArgs e) { 
        
        //find member
        ProfileCommon userProfile = Profile.GetProfile(Server.HtmlEncode(txtUsername.Text));
        Oplaco.Core.BusinessLogicLayer.AdType adtype = (Oplaco.Core.BusinessLogicLayer.AdType)Enum.Parse(typeof(Oplaco.Core.BusinessLogicLayer.AdType), ddlAdTypes.SelectedValue);
        Oplaco.Core.BusinessLogicLayer.AdLevel adlevel = (Oplaco.Core.BusinessLogicLayer.AdLevel)Enum.Parse(typeof(Oplaco.Core.BusinessLogicLayer.AdLevel), ddlAdLevel.SelectedValue);
        
        int maxItems = Int32.Parse(ddlMaxItems.SelectedValue);
        if (maxItems <= 0) maxItems = 10000;
       
        int maxPrice = 10000000; //just some high number
        if(!String.IsNullOrEmpty(txtMaxPrice.Text)) maxPrice = Int32.Parse(txtMaxPrice.Text);

        int dayrange = int.Parse(ddlDateRange.SelectedValue);
        if(dayrange<=0) dayrange = settings.MaxAdRunningDays;

        CachedAdvertismentCollection alist = AdvertismentManager.FetchByQuery(lstCategory.SelectedCategoryId, userProfile.MemberId, maxPrice, txtFullText.Text, adtype, adlevel, dayrange, false, maxItems); 
        if (alist != null && alist.Count > 0)
        {
            AdsList.DataSource = alist;
            AdsList.DataBind();
        }
        else {

            lblMessage.Text = "No results that matched your query";
            lblMessage.CssClass = "error";

            AdsList.DataSource = null;
            AdsList.DataBind();
        }
    }

    

    private void loadAdTypeListBox() {

        ListItemCollection coll = new ListItemCollection();
        ListItem item = new ListItem(Oplaco.Core.BusinessLogicLayer.AdType.ForSale.ToString(), Oplaco.Core.BusinessLogicLayer.AdType.ForSale.ToString());
        coll.Add(item);
        item = new ListItem(Oplaco.Core.BusinessLogicLayer.AdType.Wanted.ToString(), Oplaco.Core.BusinessLogicLayer.AdType.Wanted.ToString());
        coll.Add(item);
        item = new ListItem(Oplaco.Core.BusinessLogicLayer.AdType.Unspecified.ToString(), Oplaco.Core.BusinessLogicLayer.AdType.Unspecified.ToString());
        coll.Add(item);

        ddlAdTypes.DataSource = coll;
        ddlAdTypes.DataBind();
    }

    private void loadAdLevelListBox()
    {

        ListItemCollection coll = new ListItemCollection();
        ListItem item = new ListItem(Oplaco.Core.BusinessLogicLayer.AdLevel.Normal.ToString(), Oplaco.Core.BusinessLogicLayer.AdLevel.Normal.ToString());
        coll.Add(item);

        item = new ListItem(Oplaco.Core.BusinessLogicLayer.AdLevel.Featured.ToString(), Oplaco.Core.BusinessLogicLayer.AdLevel.Featured.ToString());
        coll.Add(item);
        
        item = new ListItem(Oplaco.Core.BusinessLogicLayer.AdLevel.Unspecified.ToString(), Oplaco.Core.BusinessLogicLayer.AdLevel.Unspecified.ToString());
        coll.Add(item);

        ddlAdLevel.DataSource = coll;
        ddlAdLevel.DataBind();        
    }
}